package com.hundsun.s3.common.util;


import com.hundsun.s3.common.dto.DeptDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author:肖文轩
 * @date:2018/3/5
 **/
public class DictDept {
    private final static DictDept dictDept = new DictDept();
    private static final Map<String,Object> deptMap = new HashMap<String, Object>();
    private static final Map<String,Object> pidMap = new HashMap<String, Object>();
    private static final Map<String,Object> idMap = new HashMap<String, Object>();
//    public static DictDept getInstance() {
//        return dictDept;
//    }

    /**
     * 构建字典
     */
    public static DictDept build(List<DeptDTO> list) {
        deptMap.clear();
        pidMap.clear();
        idMap.clear();
        for(DeptDTO d : list) {
            deptMap.put(String.valueOf(d.getId()),d.getDept_name());
            pidMap.put(String.valueOf(d.getId()),String.valueOf(d.getParent_id()));
            idMap.put(d.getDept_name(),String.valueOf(d.getId()));
        }
        return dictDept;
    }
    /**
     * 获取值
     * @param key
     * @return
     */
    public String getValue(String key) {
        if(key == null || key.trim().equals("") || key.equals("null")) {
            return "";
        }
        String pid = (String)pidMap.get(key);
        System.out.println(pid);
        String deptName = (String) deptMap.get(key);
        if(!pid.equals("0")) {
            return getValue(pid)+","+deptName;
        }else {
            return deptName == null ? "" : deptName;
        }
    }

    /**
     * 根据部门获取部门id
     * @return
     */
    public String getDeptId(String deptName) {
        String id = null;
        if(deptName == null || deptName.equals("") || deptName.equals("null")) {
            return "";
        }
        if(!deptName.contains(",")) {
            id = (String)idMap.get(deptName);
        }
        String[] dept = deptName.split(",");
        String deptN = dept[dept.length-1];
        if(deptMap.containsValue(deptN)) {
            id = (String)idMap.get(deptN);
        }
        return id;
    }

}
